package com.pz.scsjx;


import com.pub.dbtools.DBOperator;
import com.pub.dbtools.DataSimpDto;
import com.pub.dbtools.StaticOper;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.util.ContextHolderUtils;
import org.jeecgframework.core.util.LogUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * @Title: Controller
 * @Description:
 * @author onlineGenerator
 * @date 2017-03-22 20:11:23
 * @version V1.0
 *
 */
@Controller
@RequestMapping("/scsjxController")
public class ScsjxController extends BaseController {

    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(ScsjxController.class);

    @Autowired
    private SystemService systemService;
    /**
     * 采用minidao查询数据
     * @param request
     * @return
     */
    //scldController.do?mlist
    @RequestMapping(params = "entermain")
    public ModelAndView entermain(HttpServletRequest req) {
        req.setAttribute("m","pz/scsjx");
        return new ModelAndView("common/entermain");
    }

    /**
     * 第1表列表数据显示准备
     /fwq/com/jt/scld/entermain.jsp
     */
    @RequestMapping(params ="maintitle")
    public ModelAndView mainTitle(HttpServletRequest request) {
        System.out.print("======maintitle");
        //   List<ScldEntity> tp= DBQuerySql.findObjForJdbc("select * from f_ld_scld",1,2, com.jt.scld.entity.ScldEntity.class);

        HttpSession session = ContextHolderUtils.getSession();
        String cheight = (String) session.getAttribute("pub_pm_gd");
        String cjmheight = (String) session.getAttribute("pub_pm_jm");
        System.out.println("cheight="+cheight);

        String rdatasizes = StringUtil.getDataSize("上半屏", cheight, cjmheight,
                "50");
        String rdatasizex = StringUtil.getDataSize("下半屏", cheight, cjmheight,
                "50");
        String rdatasize = StringUtil.getDataSize("全屏", cheight, cjmheight,
                "50");
        session.setAttribute("pub_datasize", rdatasize);
        session.setAttribute("pub_datasizes", rdatasizes);
        session.setAttribute("pub_datasizex", rdatasizex);
       /* return new ModelAndView("com/jt/scld/main");*/
        return new ModelAndView("com/pz/scsjx/l");
    }

    @RequestMapping(params = "mlistDatagrid")
    public void mlistDatagrid(HttpServletRequest request, HttpServletResponse response, org.jeecgframework.core.common.model.json.DataGrid dataGrid) {
        /**
         * 注意：minidao会遵循springjdbc规则，会自动把数据库以下划线的字段，转化为驼峰写法
         * 例如数据库表字段：{USER_NAME}
         * 转化实体对应字段：{userName}
         */
        String selesql = " select a.pk_sys id,a.fd_name,a.fd_kq,a.fd_sjly,b.fd_name fd_sjlymc,a.fd_lx,a.fd_sm " +
                " from t_sjy_sys a " +
                " left join t_sjy_db b on a.fd_sjly=b.id ";

        DataSimpDto a=new DataSimpDto(selesql);

        // List<JeecgDemoEntity> list = jeecgMinidaoDao.getAllEntities(jeecgDemo, dataGrid.getPage(), dataGrid.getRows());
       /* dataGrid= scldService.getOneList(dataGrid);*/

        Integer count = a.getTotRecord();
        dataGrid.setTotal(count);
        dataGrid.setResults(a.getDataDtomap());
        dataGrid.setFields(a.getFieldName());

        // String total_salary = String.valueOf(jeecgMinidaoDao.getSumSalary());
		/*
		 * 说明：格式为 字段名:值(可选，不写该值时为分页数据的合计) 多个合计 以 , 分割
		 */

        //  dataGrid.setFooter("salary:"+(total_salary.equalsIgnoreCase("null")?"0.0":total_salary)+",age,email:合计");
        TagUtil.datagrids(response, dataGrid);
    }
    @RequestMapping(params ="subtitle")
    private String subMake(HttpSession session, HttpServletRequest request) {
        String keyid = request.getParameter("keyid");
        System.out.print("keyid="+keyid);
        String pno=request.getParameter("pno");
        System.out.print("pno="+pno);
        session.setAttribute("pub_keyid"+pno, keyid);
        session.setAttribute("pub_pno", pno);
        System.out.print("pno="+pno);
        return "/com/pz/scsjx/l"+pno;
    }
    @RequestMapping(params = "sublistDatagrid")
    public void sublistDatagrid(HttpSession session,HttpServletRequest request, HttpServletResponse response, org.jeecgframework.core.common.model.json.DataGrid dataGrid) {
        /**
         * 注意：minidao会遵循springjdbc规则，会自动把数据库以下划线的字段，转化为驼峰写法
         * 例如数据库表字段：{USER_NAME}
         * 转化实体对应字段：{userName}
         */
        String pno=(String)session.getAttribute("pub_pno");
        String keyid = (String) session.getAttribute("pub_keyid"+pno);
        String selesql="";
        if (pno.equals("0")){
            selesql = " select pk_systag id,fd_tb,fd_tag,fd_sm,fd_dtype  " +
                    " from t_sjy_systag ";
            selesql+=" where pk_sys='" + keyid + "'";
        }
        if  (pno.equals("1")){
            selesql = " select pk_systagp id,fd_mc,fd_vtype,fd_v,fd_order,fd_qzfs  " +
                   " from t_sjy_systagp  ";
           selesql+=" where pk_systag='" + keyid + "'";
       }
        if  (pno.equals("2")){
             selesql = " select pk_systagv id,fd_v,fd_sm  " +
                    " from t_sjy_systagv  ";
            selesql+=" where pk_systagp='" + keyid + "'";
        }
        DataSimpDto a=new DataSimpDto(selesql);
        Integer count = a.getTotRecord();
        dataGrid.setTotal(count);
        dataGrid.setResults(a.getDataDtomap());
        dataGrid.setFields(a.getFieldName());

        //  dataGrid.setFooter("salary:"+(total_salary.equalsIgnoreCase("null")?"0.0":total_salary)+",age,email:合计");
        TagUtil.datagrids(response, dataGrid);
    }
    
    /**
     * 新增编辑
     *
     * @return
     */
    @RequestMapping(params = "edit")
    public ModelAndView Edit(String p,String id,HttpSession session,HttpServletRequest req) {
        String t="";
        String fk="";
        
        if (StringUtil.isNotEmpty(p)){
            t=p;
            Map<String,Object> map = new HashMap<String,Object>();
            if (t.equals("0"))fk="pk_sys";
            if (t.equals("1"))fk="pk_systag";
            if (t.equals("2"))fk="pk_systagp";
  
            System.out.println("pub_keyid"+t+"="+session.getAttribute("pub_keyid"+t));
            map.put(fk,(String) session.getAttribute("pub_keyid"+t));
            req.setAttribute("t1", map);

        }
        if (StringUtil.isNotEmpty(id)) {

        	String sql="select * from t_sjy_sys where pk_sys= '"+id+"'";

            if (t.equals("0")){
            	sql="select * from t_sjy_systag where pk_systag= '"+id+"'";                
            }
            if (t.equals("1")){           	
                sql="select * from t_sjy_systagp where pk_systagp= '"+id+"'"; 
                                                                          		
            }
            if (t.equals("2")){           	
                sql="select * from t_sjy_systagv where pk_systagv= '"+id+"'"; 
                                                                          		
            }

            DataSimpDto a=new DataSimpDto(sql);
            Map<String,Object> map = new HashMap<String,Object>();
            for (int i=0;i<a.getTotField();i++){
                map.put(a.getFieldName(i),a.getDataValue(i,0));
            }
            req.setAttribute("t1", map);
        }
        return new ModelAndView("com/pz/scsjx/edit"+t);
    }

    @RequestMapping(params = "doedit")
    @ResponseBody
    public AjaxJson doEdit(String p,HttpServletRequest req) {
        AjaxJson ajaxJson = new AjaxJson();

        Hashtable<String, String> tmphash = new Hashtable<String, String>();
        Enumeration newparas = req.getParameterNames();
        String tmpfieldname = "";
        while (newparas.hasMoreElements()) {
            tmpfieldname = (String) newparas.nextElement();
            if (tmpfieldname.startsWith("pk_")||tmpfieldname.startsWith("fd_")){
                tmphash.put(tmpfieldname.toLowerCase(),
                        req.getParameter(tmpfieldname));
            }
        }

        String sql="";

        try{
            String pk="pk_sys";String tb="t_sjy_sys";
            if (StringUtil.isNotEmpty(p)){
                if (p.equals("0")){
                    pk="pk_systag";tb="t_sjy_systag";
                }
                if (p.equals("1")){
                    pk="pk_systagp";tb="t_sjy_systagp";
                }
                if (p.equals("2")){
                    pk="pk_systagv";tb="t_sjy_systagv";
                }
            }

            if (StringUtil.isNotEmpty(req.getParameter(pk))) {
                sql= StaticOper.makeUpdSql(tb,tmphash,pk,req.getParameter(pk));
            }else{
                tmphash.remove(pk);
                sql= StaticOper.makeInsSql(tb,tmphash);
            }

            systemService.executeSql(sql.toString());

        }catch (Exception e){
            LogUtil.log("保存失败", e.getMessage());
            ajaxJson.setSuccess(false);
            ajaxJson.setMsg(e.getMessage());
        }
        ajaxJson.setMsg("保存成功");
        return ajaxJson;
    }
    @RequestMapping(params = "dodel")
    @ResponseBody
    public AjaxJson doDel(String p,String id, HttpServletRequest req) {
        AjaxJson ajaxJson = new AjaxJson();

        if (StringUtil.isNotEmpty(id)) {
            ArrayList arr=new ArrayList();
            String sql="";
            if (p==null ||p.equals("")){
               sql = "delete from t_sjy_systagv where pk_systagp in (select pk_systagp from t_sjy_systagp a left join t_sjy_systag b on a.pk_systag=b.pk_systag where b.pk_sys = '"+id+"')";
               arr.add(sql);
               sql = "delete from t_sjy_systagp where pk_systag in (select pk_systag from t_sjy_systag where pk_sys = '"+id+"')";
               arr.add(sql);             
               sql = "delete from t_sjy_systag where pk_sys = '"+id+"'";
               arr.add(sql);
               sql = "delete from t_sjy_sys where pk_sys = '"+id+"'";
               arr.add(sql);          
            }
            else{
             if (p.equals("0")){
            	sql = "delete from t_sjy_systagv where pk_systagp in (select pk_systagp from t_sjy_systagp where pk_systag = '"+id+"')";            	
                arr.add(sql);
                sql = "delete from t_sjy_systagp where pk_systag = '"+id+"'";
                arr.add(sql);
                sql = "delete from t_sjy_systag where pk_systag = '"+id+"'";
                arr.add(sql);
             }
             if (p.equals("1")){
            	 sql = "delete from t_sjy_systagv where pk_systagp = '"+id+"'";
                 arr.add(sql);
                 sql = "delete from t_sjy_systagp where pk_systagp = '"+id+"'";
                 arr.add(sql);
             }
             if (p.equals("2")){
            	 sql = "delete from t_sjy_systagv where pk_systagv = '"+id+"'";
                 arr.add(sql);
             }
            }

            try {
                DBOperator.execSql(arr);
/*                systemService.executeSql(sql);
                systemService.executeSql(sql1);*/
            }catch(Exception e){
                LogUtil.log("删除失败", e.getMessage());
                ajaxJson.setSuccess(false);
                ajaxJson.setMsg(e.getMessage());
            }
            ajaxJson.setMsg("删除成功");
        }
        return ajaxJson;
    }

    
}



//    /**
//     * 新增编辑
//     *
//     * @return
//     */
//    @RequestMapping(params = "edit1")
//    public ModelAndView Edit1(String id,HttpServletRequest req) {
//        if (StringUtil.isNotEmpty(id)) {
//            String sql="select * from t_sjy_sys where pk_sys= '"+id+"'";
//            DataSimpDto a=new DataSimpDto(sql);
//
//            Map<String,Object> map = new HashMap<String,Object>();
//            map.put("pk_sys", a.getDataValue("pk_sys",0));
//            map.put("fd_name", a.getDataValue("fd_name",0) );
//            map.put("fd_kq", a.getDataValue("fd_kq",0));
//            map.put("fd_sjly", a.getDataValue("fd_sjly",0));
//            map.put("fd_lx", a.getDataValue("fd_lx",0));
//            map.put("fd_sm", a.getDataValue("fd_sm",0));
//            req.setAttribute("t1", map);
//
//        }
//        return new ModelAndView("com/pz/scsjx/edit1");
//    }
//
//    @RequestMapping(params = "doedit1")
//    @ResponseBody
//    public AjaxJson doEdit1(HttpServletRequest req) {
//        AjaxJson ajaxJson = new AjaxJson();
//        Hashtable mdate=new Hashtable();
//       
//        mdate.put("fd_name",req.getParameter("fd_name"));
//        mdate.put("fd_kq",req.getParameter("fd_kq"));
//        mdate.put("fd_sjly",req.getParameter("fd_sjly"));
//        mdate.put("fd_lx",req.getParameter("fd_lx"));
//        mdate.put("fd_sm",req.getParameter("fd_sm"));
//        String sql="";
//
//        try{
//            if (StringUtil.isNotEmpty(req.getParameter("pk_sys"))) {
//                sql= StaticOper.makeUpdSql("t_sjy_sys",mdate,"pk_sys",req.getParameter("pk_sys"));
//            }else{
//                sql= StaticOper.makeInsSql("t_sjy_sys",mdate);
//            }
//
//            systemService.executeSql(sql.toString());
//
//        }catch (Exception e){
//            LogUtil.log("数据项信息保存失败", e.getMessage());
//            ajaxJson.setSuccess(false);
//            ajaxJson.setMsg(e.getMessage());
//        }
//        ajaxJson.setMsg("数据项信息保存成功");
//        return ajaxJson;
//    }
//    @RequestMapping(params = "dodel")
//    @ResponseBody
//    public AjaxJson doDel1(String p,String id, HttpServletRequest req) {
//        AjaxJson ajaxJson = new AjaxJson();
//
//        if (StringUtil.isNotEmpty(id)) {
//            ArrayList arr=new ArrayList();
//            String sql="";
//            if (p.equals("1")){
//                sql = "delete from t_sjy_systagv where pk_systag in (select pk_systag from t_sjy_systag where pk_sys = '"+id+"')";
//                arr.add(sql);
//                sql = "delete from t_sjy_systag where pk_sys = '"+id+"'";
//                arr.add(sql);
//                sql = "delete from t_sjy_sys where pk_sys = '"+id+"'";
//                arr.add(sql);
//            }
//            if (p.equals("2")){
//                sql = "delete from t_sjy_systagv where pk_systag = '"+id+"'";
//                arr.add(sql);
//                sql = "delete from t_sjy_systag where pk_sys = '"+id+"'";
//                arr.add(sql);
//             }
//             if (p.equals("3")){
//                 sql = "delete from t_sjy_systagv where pk_systagv = '"+id+"'";
//                 arr.add(sql);
//             }
//
//            try {
//                DBOperator.execSql(arr);
///*                systemService.executeSql(sql);
//                systemService.executeSql(sql1);*/
//            }catch(Exception e){
//                LogUtil.log("删除失败", e.getMessage());
//                ajaxJson.setSuccess(false);
//                ajaxJson.setMsg(e.getMessage());
//            }
//            ajaxJson.setMsg("删除成功");
//        }
//        return ajaxJson;
//    }
//    /**
//     * 新增编辑111111
//     *
//     * @return
//     */
//    @RequestMapping(params = "edit2")
//    public ModelAndView Edit2(String id,HttpServletRequest req) {
//        if (StringUtil.isNotEmpty(id)) {
//            String sql="select * from t_sjy_sys where pk_sys= '"+id+"'";
//            DataSimpDto a=new DataSimpDto(sql);
//
//            Map<String,Object> map = new HashMap<String,Object>();
//            for (int i=0;i<a.getTotField();i++){
//            	map.put(a.getFieldName(i),a.getDataValue(i, 0));
//            }
//            
//            map.put("pk_sys", a.getDataValue("pk_sys",0));
//            map.put("fd_fwno", a.getDataValue("fd_fwno",0));
//            map.put("fd_name", a.getDataValue("fd_name",0) );
//            map.put("fd_addr", a.getDataValue("fd_addr",0));
//            map.put("fd_bz", a.getDataValue("fd_bz",0));
//            req.setAttribute("t1", map);
//
//        }
//        return new ModelAndView("com/pz/scsjx/edit1");
//    }
//
//    @RequestMapping(params = "doedit2")
//    @ResponseBody
//    public AjaxJson doEdit2(HttpServletRequest req) {
//        AjaxJson ajaxJson = new AjaxJson();
//        Hashtable mdate=new Hashtable();
//        mdate.put("fd_fwno",req.getParameter("fd_fwno"));
//        mdate.put("fd_name",req.getParameter("fd_name"));
//        mdate.put("fd_addr",req.getParameter("fd_addr"));
//        mdate.put("fd_bz",req.getParameter("fd_bz"));
//        String sql="";
//
//        try{
//            if (StringUtil.isNotEmpty(req.getParameter("pk_node"))) {
//                sql= StaticOper.makeUpdSql("t_fw_node",mdate,"pk_node",req.getParameter("pk_node"));
//            }else{
//                sql= StaticOper.makeInsSql("t_fw_node",mdate);
//            }
//
//            systemService.executeSql(sql.toString());
//
//        }catch (Exception e){
//            LogUtil.log("服务节点信息保存失败", e.getMessage());
//            ajaxJson.setSuccess(false);
//            ajaxJson.setMsg(e.getMessage());
//        }
//        ajaxJson.setMsg("服务节点信息保存成功");
//        return ajaxJson;
//    }

//}
